//
//  ZYEmoticonCollectionViewCell.swift
//  weiboZY
//
//  Created by 赵勇 on 16/5/21.
//  Copyright © 2016年 赵勇. All rights reserved.
//

import UIKit



class ZYEmoticonCollectionViewCell: UICollectionViewCell {
 
    
    // 表情模型
    var emoticons: [ZYEmoticon]? {
        
        didSet{
            
            guard let emots = emoticons else{
                return
            }
            
            // 去除重用问题
            for value in emoticomBtns {
                value.hidden = true
            }
            
            for (i,value) in emots.enumerate(){
                
                let btn = emoticomBtns[i]
                btn.hidden = false
                btn.emoticon = value
                
                // 已封装到按钮中
//                if value.type == "0" {
//                    btn.setImage(UIImage(named: value.path!), forState: .Normal)
//                    btn.setTitle(nil, forState: .Normal)
//                } else {
//                    btn.setTitle((value.code! as NSString).emoji(), forState: .Normal)
//                    btn.titleLabel?.font = UIFont.systemFontOfSize(34)
//                    btn.setImage(nil, forState: .Normal)
//                }
                
            }
            
        }
       
    }
    
    // 保存表情的按钮
    private lazy var emoticomBtns: [ZYEmoticonBtn] = [ZYEmoticonBtn]()
 
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
 
    
    // 删除按钮
    private lazy var deleEmoticonBtn:UIButton = {
    
    let btn = UIButton()
        btn.addTarget(self, action: "deleteEmoticonBtnClick", forControlEvents: .TouchUpInside)
        btn.setImage(UIImage(named: "compose_emotion_delete"), forState: .Normal)
        btn.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: .Highlighted)
        return btn
    }()
    
    
    @objc private func deleteEmoticonBtnClick() {
        
        // 点击删除按钮发送通知
        
        NSNotificationCenter.defaultCenter().postNotificationName(DidselectDelectEmoticanNotification, object: nil)
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
//        backgroundColor = RandomColor()
        addChildBtn()
        
        contentView.addSubview(deleEmoticonBtn)
        
    }
    
    
    // 添加表情按钮
    private func addChildBtn() {
        // 遍历添加20个按钮
        for _ in 0..<20 {
            
          let btn = ZYEmoticonBtn()
             btn.addTarget(self, action: "emoticonBtnClick:", forControlEvents: .TouchUpInside)
            
//            btn.backgroundColor = RandomColor()
            contentView.addSubview(btn)
            emoticomBtns.append(btn)
        }
        
        
    }
    
    override func layoutSubviews() {
        let itemWidth = width / 7
        let itemHeigth = height / 3
        
        for (i,value) in emoticomBtns.enumerate() {
            // 设置按钮大小
            value.size = CGSize(width: itemWidth, height: itemHeigth)
            // 计算col索引
            let colIndex = i % 7
            // 计算row索引
            let rowIndex = i / 7
            
            // 设置按钮的x y 坐标
            value.x = CGFloat(colIndex) * itemWidth
            value.y = CGFloat(rowIndex) * itemHeigth
            
        }
        
        
        deleEmoticonBtn.frame.size = CGSize(width: itemWidth, height: itemHeigth)
        deleEmoticonBtn.frame.origin.x = self.width - itemWidth
        deleEmoticonBtn.frame.origin.y = self.height - itemHeigth
     
    }
    
    
    @objc private func emoticonBtnClick(btn: ZYEmoticonBtn) {
        
        NSNotificationCenter.defaultCenter().postNotificationName(didSelectEmoticonBtnNotification, object: btn.emoticon)
       
    }
    
    
    
}
















